package com.xmut.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmut.commonutils.R;
import com.xmut.eduservice.entity.EduTeacher;
import com.xmut.eduservice.entity.vo.TeacherQuery;
import com.xmut.eduservice.service.EduTeacherService;
import com.xmut.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.tomcat.jni.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author huangchaoqin
 * @since 2020-07-27
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher")
@CrossOrigin//解决跨域问题
public class EduTeacherController {
    // 把service注入
    @Autowired
    private EduTeacherService eduTeacherService;

    /*
     * GET:查询；
     * POST：新增；
     * PUT：修改；
     * DELETE：删除；
     * 符合这种风格的http请求，我们就叫它RestFul；
     */

    //http://localhost:8001//eduservice/edu-teacher/findAll
    @GetMapping("getListTeacher")
    @ApiOperation(value = "所有讲师列表")
    public R findAll() {
        // 调用service的方法实现查询所有的操作
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("list", list);
    }

    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("removeTeacher/{id}")// id值通过路径进行传递
    public R removeTeacher(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable String id) {
        boolean flog = eduTeacherService.removeById(id);

        if (flog) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping("pageTeacher/{current}/{size}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long size) {


        // 创建一个page对象
        Page<EduTeacher> page = new Page<>(current, size);
        // 调用方法实现分页
        // 调用方法的时候，底层封装，把分页所有数据封装到page对象里面去
        eduTeacherService.page(page, null);

        long total = page.getTotal(); // 总记录数
        List<EduTeacher> records = page.getRecords();

        Map map = new HashMap();
        map.put("total", total);
        map.put("raws", records);

        return R.ok().data(map);
    }


    // 多条件组合查询带分页
    @PostMapping("pageTeacherCondition/{current}/{size}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long size,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        // 创建一个page对象
        Page<EduTeacher> page = new Page<>(current, size);
//        System.out.println("-------------" + current);
        // wapper
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis 动态sql
        // 判断条件值是否为空，如果不为空拼接条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();

        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (null != level) {
            queryWrapper.eq("level", level);
        }
//        System.err.println("+++++++++++++" + level);
        ;
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }

        queryWrapper.orderByDesc("gmt_modified");
        // 调用方法实现分页
        // 调用方法的时候，底层封装，把分页所有数据封装到page对象里面去
        eduTeacherService.page(page, queryWrapper);

        long total = page.getTotal(); // 总记录数

        List<EduTeacher> records = page.getRecords();

        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return R.ok().data(map);
    }

    // 添加讲师方法
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    // 根据讲师id进行查询
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);


        return R.ok().data("teacher", eduTeacher);
    }

    // 讲师修改功能
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.updateById(eduTeacher);

        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }

    }
    // 讲师修改功能
//    @ApiOperation(value = "根据ID修改讲师")
//    @PutMapping("updateTeacher/{id}")
//    public R updateTeacher(
//            @ApiParam(name = "id", value = "讲师ID", required = true)
//            @PathVariable String id,
//            @ApiParam(name = "teacher", value = "讲师对象", required = true)
//            @RequestBody EduTeacher teacher) {
//        teacher.setId(id);
//        eduTeacherService.updateById(teacher);
//        return R.ok();
//    }
}

